package com.wjf.birdfish.util;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.MutablePropertyValues;
import org.springframework.util.Assert;
import org.springframework.validation.DataBinder;




/** 
 * Created on 2006-10-28
 * <p>Title: 自行封装的一个BeanUtils, 提供Spring, Apache的BeanUtils 之外的一些功能.</p>
 * <p/>
 * Description: 访问在当前类声明的private/protected成员变量及private/protected函数的BeanUtils.
 * 注意,因为必须为当前类声明的变量,通过继承获得的protected变量将不能访问,
 * 需要转型到声明该变量的类才能访问.
 * 反射的其他功能请使用Apache Jarkarta Commons BeanUtils
 * <p>Copyright: Copyright TYDIC (c) 2006</p>
 * <p>Company: tydic.com</p>
 * @author <a href="malito:tongys@tydic.com">童永胜</a>
 * @version 1.0
 */
public class BeanUtils {
    /**
     * 获取当前类声明的private/protected变量
     */
    static public Object getPrivateProperty(Object object, String propertyName) throws IllegalAccessException, NoSuchFieldException {
        Assert.notNull(object);
        Assert.hasText(propertyName);
        Field field = object.getClass().getDeclaredField(propertyName);
        field.setAccessible(true);
        return field.get(object);
    }

    /**
     * 设置当前类声明的private/protected变量
     */
    static public void setPrivateProperty(Object object, String propertyName, Object newValue) throws IllegalAccessException, NoSuchFieldException {
        Assert.notNull(object);
        Assert.hasText(propertyName);

        Field field = object.getClass().getDeclaredField(propertyName);
        field.setAccessible(true);
        field.set(object, newValue);
    }

    /**
     * 调用当前类声明的private/protected函数
     */
    static public Object invokePrivateMethod(Object object, String methodName, Object... params) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        Assert.notNull(object);
        Assert.hasText(methodName);
        Class[] types = new Class[params.length];
        for (int i = 0; i < params.length; i++) {
            types[i] = params[i].getClass();
        }
        Method method = object.getClass().getDeclaredMethod(methodName, types);
        method.setAccessible(true);
        return method.invoke(object, params);
    }
    
    /**
     * 生成
     * @param clzss
     * @return
     */
    public static String getObjectToString(Class clzss) {
    	Object obj;
        try {
            obj = clzss.newInstance();
            Map map = org.apache.commons.beanutils.BeanUtils.describe(obj);
            map.remove("class");
            String signature = "\npublic String toString(){\n";
            StringBuffer sb = new StringBuffer(signature);
            String newSb = "\tStringBuffer sb = new StringBuffer()\n";
            sb.append(newSb);
            for (Iterator it = map.keySet().iterator(); it.hasNext();) {
                String key = (String) it.next();
                String str = "\t\t.append(" + key + ").append(\"\\t\")\n";
                sb.append(str);
            }
            sb.append("\t;\n").append("\treturn sb.toString();\n").append("}");
            return sb.toString();
        } catch (Throwable t) {
            t.printStackTrace();
        }
        return null;
    }
    
	public static <T> T bind(Map<String, Object> mapping, Class<T> commandType) {
		T command = null;
		try {
			command = commandType.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
		}
		DataBinder dataBinder = new DataBinder(command);
		dataBinder.bind(new MutablePropertyValues(mapping));
		return command;
	}

//	public static <T> T bind(HttpServletRequest request, Class<T> commandType) {
//		return bind((Map<String, Object>) request.getParameterMap(), commandType);
//	}
    
    public static void main(String[] args) {
	}
}
